import { _decorator, Component, input, Input, instantiate, math, Node, Prefab } from 'cc';
import { GameManager } from './GameManager';
import { Enemy } from './Enemy';
const { ccclass, property } = _decorator;

@ccclass('EnemyManager')
export class EnemyManager extends Component {

    private static instance: EnemyManager;

    public static getInstance(): EnemyManager {
        return this.instance;
    }

    @property
    enemy0SpawnRate: number = 1;
    @property(Prefab)
    enemy0Prefab: Prefab = null;

    @property
    enemy1SpawnRate: number = 3;
    @property(Prefab)
    enemy1Prefab: Prefab = null;

    @property
    enemy2SpawnRate: number = 5;
    @property(Prefab)
    enemy2Prefab: Prefab = null;

    @property
    rewardSpawnRate: number = 15;
    @property(Prefab)
    reward1Prefab: Prefab = null;
    @property(Prefab)
    reward2Prefab: Prefab = null;

    doubleClickIntrval: number = 0.3;
    lastClickTime: number = 0;

    @property([Node])
    enemyArray: Node[] = [];

    protected onLoad(): void {
        this.lastClickTime = 0;
        input.on(Input.EventType.TOUCH_END, this.onTouchEnd, this);
    }


    start() {
        EnemyManager.instance = this;
        this.schedule(this.enemy0Spawn, this.enemy0SpawnRate);
        this.schedule(this.enemy1Spawn, this.enemy1SpawnRate);
        this.schedule(this.enemy2Spawn, this.enemy2SpawnRate);
        this.schedule(this.rewardSpawn, this.rewardSpawnRate);
    }

    update(deltaTime: number) {

    }

    protected onDestroy(): void {
        this.unschedule(this.enemy0Spawn)
        this.unschedule(this.enemy1Spawn)
        this.unschedule(this.enemy2Spawn)
        this.node.off(Input.EventType.TOUCH_END, this.onTouchEnd, this);
    }

    enemy0Spawn() {
        const enemyNode = this.Spawn(this.enemy0Prefab, -215, 215, 450);
        this.enemyArray.push(enemyNode);
    }

    enemy1Spawn() {
        const enemyNode = this.Spawn(this.enemy1Prefab, -215, 215, 475);
        this.enemyArray.push(enemyNode);
    }
    enemy2Spawn() {
        const enemyNode = this.Spawn(this.enemy2Prefab, -150, 150, 560);
        this.enemyArray.push(enemyNode);
    }

    rewardSpawn() {
        const randomNumber = math.randomRangeInt(0, 2);
        let choseprefab = null;
        if (randomNumber == 0) {
            choseprefab = this.reward1Prefab
        } else {
            choseprefab = this.reward2Prefab
        }
        this.Spawn(choseprefab, -207, 207, 474);
    }


    Spawn(downPrefab: Prefab, minX: number, maxX: number, Y: number): Node {
        const prefab = instantiate(downPrefab)
        this.node.addChild(prefab)
        const randomX = math.randomRangeInt(minX, maxX);
        prefab.setPosition(randomX, Y, 0);
        return prefab;

    }

    onTouchEnd(event) {
        let currentTime = Date.now();
        let timeDiff = (currentTime - this.lastClickTime) / 1000;

        if (timeDiff < this.doubleClickIntrval) {
            this.onDoubleClick(event);
        }

        this.lastClickTime = currentTime;
    }
    onDoubleClick(event) {
        //双击后执行操作
        console.log("Double click action executed");
       
        if (!GameManager.getInstance().isHaveBomb()){
            console.log(GameManager.getInstance().GetBombNumber());
            console.log("None Bomb");
            return;
        } 

        GameManager.getInstance().useBomb();

        for(let enemy of this.enemyArray){
           const e= enemy.getComponent(Enemy);
           e.killNow();
        }
    }



    removeEnemy(n: Node) {
        const index = this.enemyArray.indexOf(n);
        if (index !== -1) {
            this.enemyArray.splice(index, 1);
        }
    }
}


